package com.hrzyit.whistle.user.ui.activity

import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.hrzyit.base.ext.enable
import com.hrzyit.base.ext.onClick
import com.hrzyit.base.ui.activity.BaseMvpActivity
import com.hrzyit.provider.common.ProviderConstant
import com.hrzyit.provider.routers.RouterPath
import com.hrzyit.whistle.user.R
import com.hrzyit.whistle.user.data.bean.UserInfo
import com.hrzyit.whistle.user.injection.component.DaggerUserComponent
import com.hrzyit.whistle.user.injection.module.UserModule
import com.hrzyit.whistle.user.presenter.LoginPresenter
import com.hrzyit.whistle.user.presenter.view.LoginView
import com.hrzyit.whistle.user.utils.UserPrefsUtils
import com.hrzyit.base.utils.digest.AESUtils
import kotlinx.android.synthetic.main.activity_login.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

/**
 * 登录 Activity
 * @author YangWei
 * @date 2019/3/10
 */
@Route(path = RouterPath.UserCenter.PATH_LOGIN)
class LoginActivity : BaseMvpActivity<LoginPresenter>(), LoginView, View.OnClickListener {

    private lateinit var secretKey: ByteArray

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        secretKey = AESUtils.generatePassword(this)

        // 初始化视图
        initView()

        // 初始化记住我功能
        initRememberMe()
    }

    /**
     * 初始化记住我功能
     */
    private fun initRememberMe() {

        val loginUserInfo = UserPrefsUtils.getRememberMeInfo(secretKey)

        if (loginUserInfo.isNullOrEmpty().not()) {
            when (val isRememberMe = loginUserInfo[ProviderConstant.R_IS_REMEMBER_ME] as Boolean) {
                true -> {
                    // 已选择过记住我功能，回显示数据
                    mMobileEt.setText(loginUserInfo[ProviderConstant.R_KEY_MOBILE] as String)
                    mPwdEt.setText(loginUserInfo[ProviderConstant.R_KEY_PWD] as String)
                    mNameEt.setText(loginUserInfo[ProviderConstant.R_KEY_ACCOUNT] as String)
                    mRememberMeBtn.isChecked = isRememberMe
                }
            }
        }
    }

    /**
     * 初始化视图
     */
    private fun initView() {
        // 必填项输入完成后登录按钮可点击
        with(mLoginBtn) {
            enable(mMobileEt) { isBtnEnable() }
            enable(mNameEt) { isBtnEnable() }
            enable(mPwdEt) { isBtnEnable() }
        }

        mLoginBtn.onClick(this)
        mForgetPwdBtn.onClick(this)
    }

    /**
     * 使用扩展方法注册点击事件
     */
    override fun onClick(view: View) {
        when (view.id) {
            R.id.mLoginBtn -> { // 用户登录
                val mobile = mMobileEt.text.toString()
                val account = mNameEt.text.toString()
                // TODO 密码如何处理？2019-03-28
                val pwd = mPwdEt.text.toString()
                // TODO 后续集成极光
                val pushId = ""
                mPresenter.login(mobile, account, pwd, pushId)
            }
            R.id.mForgetPwdBtn -> { // 忘记密码
                startActivity<ForgetPwdActivity>()
            }
        }
    }

    /**
     * 判断按钮是否可用
     */
    private fun isBtnEnable(): Boolean {
        return mMobileEt.text.isNullOrEmpty().not() &&
                mPwdEt.text.isNullOrEmpty().not() &&
                mNameEt.text.isNullOrEmpty().not()
    }

    /**
     * Dagger2 注册
     */
    override fun injectComponent() {

        DaggerUserComponent.builder()
            .activityComponent(mActivityComponent)
            .userModule(UserModule())
            .build()
            .inject(this)

        // 初始化 Presenter.View
        mPresenter.mView = this
    }



    /**
     * 登录完成后回调
     */
    override fun onLoginResult(result: UserInfo) {
        toast("登录成功")
        UserPrefsUtils.putUserInfo(result)

        // 如果当前用户选择"记住我"功能，本地帐号信息
        val pwd = mPwdEt.text.toString()

        val account = mNameEt.text.toString()

        val mobile = mMobileEt.text.toString()

        UserPrefsUtils.putRememberMeInfo(mRememberMeBtn.isChecked,pwd, account,mobile,secretKey)

        ARouter.getInstance().build(RouterPath.WhistleApp.PATH_MAIN)
            .navigation()
    }
}